import {getToken} from '@/utils/auth'
import store from './store'
import {isWechat} from "./utils/validate";

// 登录页面
const loginPage = "/pages/login"

// 注册页面
const registerPage = "/pages/register"

// 页面登录白名单
const whiteList = [
    '/pages/login',//公共登录
	'/pages/register',//注册
	'/pages/forgetPassword',//找回密码
	'/pages/recoverAccount',//找回账号
    '/pages/common/policy/userAgreement',// 同意协议
    '/pages/authentication/companyRealname/welcome',
    '/pages/authentication/companyRealname/adminChange',// 管理员变更 二次授权
    '/pages/signWeb/loginWeb',// 签署登录页
    '/pages/common/policy/privacyPolicy', //隐私协议
    '/pages/common/policy/eleBuyAgreement', // 电子签约服务购买协议
    '/pages/authentication/personal/facePersonal',// 扫脸页面
    '/pages/authentication/personal/certifyLegalPerson',// 法人认证落地页
    '/pages/forWordAdmin',//转交管理员落地页
]

// 用户认证白名单
const certifyList = [
    '/pages/authentication/personal/personal',// 认证页面1
    '/pages/authentication/personal/facePersonal',// 认证页面人脸
    '/pages/authentication/personal/authentication',// 认证页面2
    '/pages/common/imageCropper/index',// 证件剪切页面
    '/pages/common/policy/userAgreement',// 同意协议
    '/pages/common/policy/privacyPolicy',
    '/pages/common/policy/eleBuyAgreement',// 电子签约服务购买协议
    '/pages/index',// 首页不拦截
]

// 检查地址白名单
function checkWhite(url) {
    const path = url.split('?')[0]
    return whiteList.indexOf(path) !== -1
}

// 检查认证白名单
function checkCertifyWhite(url) {
    const path = url.split('?')[0]
    return certifyList.indexOf(path) !== -1
}

// 页面跳转验证拦截器 "navigateTo",
let list = ["navigateTo","redirectTo", "reLaunch", "switchTab"]
list.forEach(item => {
    uni.addInterceptor(item, {
        invoke(to) {
            if (getToken()) {
                // 未认证
                if (store.getters.staffInfo.isCertify == 0 && !checkCertifyWhite(to.url)) {
                    uni.showModal({
                        title: '未实名认证',
						confirmText:'立即认证',
                        content: '1、为确保您通过励氪签平台签署的文件符合国家法律、法规要求以及励氪签为您申请制作的电子签名为您本人专有，您需要先进行个人实名认证；\n2、申请人需年满16周岁，仅支持持有：中国居民身份证、港澳台居民来往内地通行证、中国港澳台居民居住证、外国人居住证的个人进行实名认证；',
                        success: function (res) {
                            if (res.confirm) {
                                uni.reLaunch({url: "/pages/authentication/personal/personal"})
                            } else if (res.cancel) {

                            }
                        }
                    });
                    return false;
                }
                if (to.path === loginPage) {
                    uni.reLaunch({url: "/"})
                }
                return true
            } else {

                // 若是微信浏览器 则获取code
                if (isWechat()) {
                    // 判断微信授权code是否存在
                    let wechatAuthCode = uni.getStorageSync("wechatAuthCode");
                    if (!wechatAuthCode) {
                        console.log(111111)
                    }
                }
                if (checkWhite(to.url)) {
                    return true
                }
                uni.reLaunch({url: loginPage})
                return false
            }
        },
        fail(err) {
            uni.showModal({
                content: JSON.stringify(err),
                showCancel: false
            });
            console.log(err)
        }
    })
})
export default {
    whiteList
}
